package com.ai.web.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.h2.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ai.web.service.ForumFansReplyService;
import com.ai.web.vo.ForumFansReplyV;
import com.ai.web.vo.QueryData;
import com.ai.web.vo.ResultModel;
/**
 * 粉丝用户交流区
 * 回复帖子
* @ClassName: forumFansReplyController 
* @Description: TODO 
* @author:  
* @date: 2014年3月24日
*
 */
@Controller
@RequestMapping("/forumFansReply")
public class ForumFansReplyController extends BaseController {
    

    private static Logger logger = Logger.getLogger(ForumFansReplyController.class);
    
    @Autowired
    private ForumFansReplyService forumFansReplyService;
    
    @RequestMapping(value = "/list")
    public String list() {
        
        logger.info("hello......test");
        return "forumFansReply/forumFansReply_list";
    }
    
    @RequestMapping(value = "/query", produces = "application/json")
    @ResponseBody
    public QueryData query(@RequestParam HashMap<String, Object> paramMap)
            throws Exception {
        
        Map<String, Object> conditionMap = buildCondition(paramMap);
        logger.info("---- " + conditionMap);
        
        QueryData qd = forumFansReplyService.queryListAndCount(conditionMap);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<ForumFansReplyV> dataList = new ArrayList<ForumFansReplyV>();
        for(Object object:qd.getDataList()){
        	ForumFansReplyV forumFansReplyV = (ForumFansReplyV)object;
        	if(forumFansReplyV.getPostDate() != null){
        		forumFansReplyV.setPostDateStr(sdf.format(forumFansReplyV.getPostDate()));
        	}
            dataList.add(forumFansReplyV);
        }
        qd.setDataList(dataList);
        return qd;
    }
    
    @RequestMapping(value = "/new")
    public String newforumFansReply(Model model) {
        
        ForumFansReplyV forumFansReplyV = new ForumFansReplyV();
        model.addAttribute(forumFansReplyV);
        
        return "forumFansReply/forumFansReply_template";
    }

    
    @RequestMapping(value = "/update/{id}")
    public String updateforumFansReply(@PathVariable("id") int id, Model model)
            throws Exception {
   	 	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ForumFansReplyV forumFansReplyV = forumFansReplyService.getForumFansReply(id);
       	if(forumFansReplyV.getPostDate() != null){
    		forumFansReplyV.setPostDateStr(sdf.format(forumFansReplyV.getPostDate()));
    	}
        logger.info("----- getUser:" + forumFansReplyV);
        
        model.addAttribute(forumFansReplyV);
        
        return "forumFansReply/forumFansReply_template";
    }
    
    @RequestMapping(value = "/save", produces = "application/json")
    @ResponseBody
    public ResultModel saveforumFansReply(ForumFansReplyV forumFansReplyV) throws Exception {
   	 	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        logger.info("-----save:" + forumFansReplyV);
        if(!StringUtils.isNullOrEmpty(forumFansReplyV.getPostDateStr())){
        	forumFansReplyV.setPostDate(sdf.parse(forumFansReplyV.getPostDateStr()));
        }
        ResultModel rm = forumFansReplyService.saveForumFansReply(forumFansReplyV);
        
        return rm;
    }
    
    @RequestMapping(value = "/delete", produces = "application/json")
    @ResponseBody
    public ResultModel deleteforumFansReply(@RequestParam String delIds)
            throws Exception {
        
        System.out.println(delIds);
        
        List<Integer> userIds = getDeleteIds(delIds);
        ResultModel rm = forumFansReplyService.deleteForumFansReplys(userIds);
        
        return rm;
    }
    
    
}
